﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;

namespace GE_Management_Tool.Settings
{
    public partial class frmA0031_Form_Edit_Page : frmBase, ICategory<CForm_Tree>
    {
        int m_iParent_ID = 0;
        CForm_Tree m_objFormTree;
        Boolean isNew = false;

        public frmA0031_Form_Edit_Page()
        {
            InitializeComponent();
        }

        private void frmA0031_Form_Edit_Page_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string error = Check_Before_Save();
                if (error != "")
                {
                    throw new Exception(error);
                }
                if (isNew == true)
                {
                    m_objFormTree = new CForm_Tree();
                    Assign_Control_To_Obj();
                    CForm_Tree_Controller v_objCtrlForm_Tree = new CForm_Tree_Controller();
                    v_objCtrlForm_Tree.Insert_Form_Tree(m_objFormTree);
                }
                else
                {
                    Assign_Control_To_Obj();
                    CForm_Tree_Controller v_objCtrlForm_Tree = new CForm_Tree_Controller();
                    v_objCtrlForm_Tree.Insert_Form_Tree(m_objFormTree);
                }


            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        public CForm_Tree Show_To_Create()
        {
            return m_objFormTree;
        }

        public CForm_Tree Show_To_Create(int p_iParent_ID)
        {
            isNew = true;
            m_iParent_ID = p_iParent_ID;
            this.ShowDialog();

            return m_objFormTree;
        }

        public CForm_Tree Show_To_Edit(CForm_Tree pobjData, ref bool pisChange)
        {
            isNew = false;

            return m_objFormTree;
        }

        public void Assign_Control_To_Obj()
        {
            m_objFormTree.Parent_ID = m_iParent_ID;
            m_objFormTree.Stage = txtFormName.Text;
            m_objFormTree.Form_ID = txtForm_ID.Text;
        }

        public void Assign_Obj_To_Control(CForm_Tree pobjData)
        {
            txtForm_ID.Text = pobjData.Form_ID;
            txtFormName.Text = pobjData.Stage;
        }

        public string Check_Before_Save()
        {
            StringBuilder v_sb = new StringBuilder();
            if (txtForm_ID.Text == "")
            {
                v_sb.AppendLine("-Missing Form ID.");
            }
            if (txtFormName.Text == "")
            {
                v_sb.AppendLine("-Missing Form Name.");
            }

            return v_sb.ToString();
        }
    }
}